﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Dashboard.Expense.Business;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;

namespace Dashboard.Expense
{
    public partial class FormExchangeRates : Form
    {
        public FormExchangeRates()
        {
            InitializeComponent();
        }

        private void FormUnproductiveTime_Load(object sender, EventArgs e)
        {
            this.BindYear();

            this.QueryData();
        }

        private void BindYear()
        {
            int yearBegin = 2011;
            for (int i = 0; i < 20; i++)
            {
                this.comboBoxYear.Items.Add(yearBegin+i);
            }

            this.comboBoxYear.SelectedItem = DateTime.Now.Year;
        }
        
        private void toolStripButtonQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }

        private void QueryData()
        {
            try
            {
                Exp_ExchangeRate eer = new Exp_ExchangeRate();
                DataTable rates = eer.GenerateDataSourceForExchangeRate(int.Parse(this.comboBoxYear.Text));

                this.ultraGridMain.DataSource = rates;
                this.ultraGridMain.DataBind();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButtonCreate_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                this.ultraGridMain.UpdateData();

                if (this.ultraGridMain.Rows.Count == 0)
                {
                    MessageBox.Show(this, "No data found in the grid!", "Info", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                    return;
                }

                var data = new Dictionary<string, Dictionary<int, decimal>>();
                foreach (UltraGridRow row in this.ultraGridMain.Rows)
                {
                    string currency = row.Cells["Currency"].Value.ToString();
                    var dict = new Dictionary<int, decimal>();
                    foreach (UltraGridCell cell in row.Cells)
                    {
                        if (cell.Column.Key == "Currency")
                        {
                            continue;
                        }

                        if (cell.OriginalValue != cell.Value && decimal.Parse(cell.Value.ToString()) != 0)
                        {
                            dict.Add(int.Parse(cell.Column.Key), decimal.Parse(cell.Value.ToString()));
                        }
                    }
                    data.Add(currency, dict);
                }

                // Save
                new Exp_ExchangeRate().SaveExchangeRates(data, int.Parse(this.comboBoxYear.Text), CommonBusiness.GetCurrentLoginUser());

                MessageBox.Show("Save data successfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void ultraGridMain_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            // 自适应列宽
            e.Layout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.ResizeAllColumns;
            // 是否允许调整列宽
            e.Layout.Override.AllowColSizing = AllowColSizing.Free;
            // 是否允许移动栏位顺序
            e.Layout.Override.AllowColMoving = AllowColMoving.NotAllowed;

            // 设置Grid的Split窗口个数，建议设置为1--不允许Split
            e.Layout.MaxColScrollRegions = 1;
            e.Layout.MaxRowScrollRegions = 1;

            // 冻结列
            e.Layout.UseFixedHeaders = false;

            // 排序
            e.Layout.Override.HeaderClickAction = HeaderClickAction.Select;

            // 不允许删除
            e.Layout.Override.AllowDelete = DefaultableBoolean.False;

            // 滚动提示
            e.Layout.Bands[0].ScrollTipField = "Currency";
            
            for (int i = 0; i < e.Layout.Bands[0].Columns.Count; i++)
            {
                if (e.Layout.Bands[0].Columns[i].Key == "Currency")
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.NoEdit;
                }
                else
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.AllowEdit;
                    e.Layout.Bands[0].Columns[i].CellClickAction = CellClickAction.EditAndSelectText;
                    e.Layout.Bands[0].Columns[i].CellAppearance.TextHAlign = HAlign.Right;
                    e.Layout.Bands[0].Columns[i].MaskInput = "{double:4.3}";
                }
            }
        }

        private void toolStripButtonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBoxYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.QueryData();
        }

    }
}
